//
//  PersonalTurnViewController.m
//  Nurse
//
//  Created by 张攀 on 13-12-18.
//  Copyright (c) 2013年 RainSets. All rights reserved.
//

#import "PersonalTurnViewController.h"

#import "PersonalTurnViewCell.h"
#import "PatientService.h"
#import "OperatorService.h"
#import "BedViewController.h"
#import "NurseService.h"
@interface PersonalTurnViewController ()
{
    UIPickerView *_pickerview;
    UIToolbar *_toolbar;
    UITextField *_textfield;
    NSArray *pickerData;
    NSMutableDictionary *ksDictionary;
    NSMutableDictionary *bqDictionary;

}

@end
NSString *xm;
NSString *zyh;
NSString *cwh;
NSString *ssbq;
NSString *ssks;
NSString *ksdm;
NSString *bqdm;
NSString *xb;
NSString *title;
NSString *nksdm;
NSString *nbqdm;
NSString *ncwh;
@implementation PersonalTurnViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
  //  [self.tpScroll addSubview:contentView];
    
   // [self.TpScroll contentSizeToFit];
    // Do any additional setup after loading the view from its nib.
    self.title  = @"流转";
    
    
//    NSArray * ibs2 = [[NSBundle mainBundle] loadNibNamed:@"PersonalTurnViewController" owner:self options:nil];
//    UIView *contentView = [ibs2 lastObject];
//    [self.TpScroll addSubview:contentView];
    
    //[self.TpScroll contentSizeToFit];
    [self setleftBarButtonItem:@""orImaged:@"head-icon_04.png" Event:@selector(leftBar_clicked:)];
    self.view.backgroundColor=[UIColor colorWithRed:235/255.0 green:239/255.0 blue:245/255.0 alpha:1.0];

    
    
    [self.Btnsure setTitle:@"确认" forState:UIControlStateNormal];
    [self.Btnsure setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.Btnsure.backgroundColor=[UIColor colorWithRed:76/255.0 green:214/255.0 blue:193/255.0 alpha:1.0];
   // [self.Btnsure setBackgroundImage:[UIImage imageNamed:@"head-bg.png"] forState:UIControlStateNormal];
    NSDictionary* patient = [[PatientService sharedInstance]currentPatient];
    
    xm= [patient valueForKey:@"姓名"];
    zyh= [patient valueForKey:@"住院号"];
    cwh=[patient valueForKey:@"床位号"];
    ssbq=[patient valueForKey:@"所属病区"];
    xb= [patient valueForKey:@"性别"];
    bqdm=[patient valueForKey:@"病区代码"];
    ssks=[patient valueForKey:@"所属科室"];
    ksdm=[patient valueForKey:@"科室代码"];
    //AppDelegate  *appDelegate = App;
    if ([bqdm isEqualToString:[OperatorService sharedInstance].currentOperatorWardCode])
        {
            self.title=@"转出";
            
        }
    else
       {
            self.title=@"转入";
    
       }
 
    [self setdata];
}


-(void)Arrangement:(NSString *)strBz
{
    // self.Uizy.frame.origin.y=244+84f;
//    // self.Uizy.frame.origin.y==244+84;
    if([strBz isEqual:@"1"])
   {
       CGPoint point = CGPointMake(0.0f, 20.0f);
       [self.Scrollview setContentOffset:point animated:YES];
        [self.Uixzbq setTop:(240+224)];
        [self.Uixzcw setTop:(300+224)];
       [self.Btnsure setTop:(373+224)];
       
    }
    
    if([strBz isEqual:@"2"])
    {
        
        CGPoint point = CGPointMake(0.0f, 80.0f);
        [self.Scrollview setContentOffset:point animated:YES];
        [self.Uixzcw setTop:300+224];
        
        [self.Btnsure setTop:(373+224)];
        
    }
    if([strBz isEqual:@"3"])
    {
        //[self.Uixzcw setTop:300+244];
        // [self setOffset];
        
        CGPoint point = CGPointMake(0.0f, 140.0f);
        [self.Scrollview setContentOffset:point animated:YES];
        [self.Btnsure setTop:(373+224)];
        
    }
    if([strBz isEqual:@"0"])
    {
        
        CGPoint point = CGPointMake(0.0f, 0.0f);
        [self.Scrollview setContentOffset:point animated:YES];
        [self.Uixzbq setTop:(240)];
        [self.Uixzcw setTop:(300)];
        [self.Btnsure setTop:(373)];

        
    }
}


-(void)setOffset
{
    
    CGPoint newScrollViewContentOffset = self.Scrollview.contentOffset;
    float yd;
    yd=504+244-newScrollViewContentOffset.y-504;
    newScrollViewContentOffset.y =newScrollViewContentOffset.y+50;
    //self.Scrollview setContentOffset:<#(CGPoint)#> animated:<#(BOOL)#>
    //最后设置scollView's contentOffset
     //= newScrollViewContentOffset;
    [self.Scrollview setContentOffset:newScrollViewContentOffset animated:YES];
    
}


-(void)setdata
{
    self.lblbrxm.text=xm;
    if ([xb isEqualToString:@"女"]) {
        self.imgbrxb.image=[UIImage imageNamed:@"gender-female2.png"];
        
    }
    else{
         self.imgbrxb.image=[UIImage imageNamed:@"gender-male2.png"];
    }
    self.lblssbq.text=ssbq;
    self.lblssks.text=ssks;
    self.lblcwh.text=cwh;
    self.lbltype.text=self.title;

}
//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [pickerData count];
}
//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [pickerData objectAtIndex:row];
}
//调整pickerview高度

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component

{
    
    return 30.0;
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger) row
       inComponent:(NSInteger)component {
    //_textfield.text= [pickerData objectAtIndex:row];
    
}



-(void)leftBar_clicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}
-(void)ksPicker
{
    [self Arrangement:@"0"];
    NSInteger row =[_pickerview selectedRowInComponent:0];
    NSString *selected = [pickerData objectAtIndex:row];
    self.lblksxz.text=selected;
    nksdm=[ksDictionary objectForKey:selected];
    self.lblksxz.textColor=[UIColor darkTextColor];
    [_pickerview removeFromSuperview];
    [_toolbar removeFromSuperview];
    _pickerview = nil;
    self.Scrollview.scrollEnabled=YES;
    
   // self.view.
}

-(void)bqPicker
{
    [self Arrangement:@"0"];
    NSInteger row =[_pickerview selectedRowInComponent:0];
    NSString *selected = [pickerData objectAtIndex:row];
    self.lblbqxz.text=selected;
    nbqdm=[bqDictionary objectForKey:selected];
    self.lblbqxz.textColor=[UIColor darkTextColor];
    [_pickerview removeFromSuperview];
    [_toolbar removeFromSuperview];
    _pickerview = nil;
    self.Scrollview.scrollEnabled=YES;
    
}
-(void)cwPicker
{
    [self Arrangement:@"0"];
    NSInteger row =[_pickerview selectedRowInComponent:0];
    NSString *selected = [pickerData objectAtIndex:row];
    self.lblcwxz.text=selected;
    ncwh=selected;
    //bqdm=[bqDictionary objectForKey:selected];
    self.lblcwxz.textColor=[UIColor darkTextColor];
    [_pickerview removeFromSuperview];
    [_toolbar removeFromSuperview];
    _pickerview = nil;
    self.Scrollview.scrollEnabled=YES;
    
}

- (IBAction)Btks:(id)sender {
    
    [self Arrangement:@"1"];
    if (!_pickerview) {
        
        //[self Arrangement:@"2"];
        // 建立 UIToolbar
        _toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,240-20, 320, 44)];
        // 選取日期完成鈕 並給他一個 selector
        UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonSystemItemDone target:self action:@selector(ksPicker)];
        UIBarButtonItem *Flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        //        _textfield = [[UITextField alloc] initWithFrame:CGRectMake(20, 5, 100, 30)];
        //        _textfield.backgroundColor = [UIColor whiteColor];
        //        _textfield.font=[UIFont systemFontOfSize:13];
        //        _textfield.delegate=self;
        //
        //        UIBarButtonItem *firstTextField=[[UIBarButtonItem alloc] initWithCustomView:_textfield];
        
        // 把按鈕加進 UIToolbar
        //154,238,225
        right.tintColor=[UIColor redColor];
        _toolbar.backgroundColor=[UIColor colorWithRed:180/255.0 green:190/255.0 blue:191/255.0 alpha:1];
        [_toolbar setItems:@[Flex,right] animated:YES];
        _pickerview = [[UIPickerView alloc] init];
        _pickerview.delegate =self;
        _pickerview.frame = CGRectMake(0, 240+44-20, 320, 180);
        
        
        [[NurseService sharedInstance]queryDepartment:ssbq withSuccessHandler:^(id result) {
            
            if ([result isKindOfClass:[NSArray class]])
            {
                NSMutableArray *dataArray=[NSMutableArray array];

                NSMutableDictionary *KsDict=[NSMutableDictionary dictionary];
              
//                dataArray=[result objectForKey:@"科室名称"];
          for(int i = 0; i < [result count]; i++)
               {
                   
                NSDictionary* record = [result objectAtIndex:i];
                   NSString*ksmc=[record valueForKey:@"科室名称"];
                   NSString*ksdm=[record valueForKey:@"科室代码"];
                  [dataArray addObject:ksmc];
                 
                  [KsDict setObject: ksdm forKey:ksmc];
               
              }
            pickerData=dataArray;
            ksDictionary=KsDict;
            //NSString*dm=[ksDictionary objectForKey:@"普外科"];
           //NSString*dm1=[ksDictionary objectForKey:@"10100000"];
            [self.view addSubview:_toolbar];
            [self.view addSubview:_pickerview];
                // [self.view addSubview:lbl];
                self.lblksxz.textColor=[UIColor colorWithRed:255/255.0 green:0/255.0 blue:0/255.0 alpha:1];
                _pickerview.backgroundColor = [UIColor whiteColor];
                
               
             }
        }andFailHandler:^(NSError *error, NSString *title) {
            [[DataEngine sharedInstance]showMessage:[error description] withTitle:title];
        }];
        

        self.Scrollview.scrollEnabled=false;

  }
}

- (IBAction)Btbq:(id)sender {
    
    
    
    if(nksdm==nil)
    {
        [[DataEngine sharedInstance]showMessage:@"请先选择科室!" withTitle:@"病区选择错误"];
    }
    else
    {
    [self Arrangement:@"2"];
    if (!_pickerview) {
        
        //[self Arrangement:@"2"];
        // 建立 UIToolbar
        _toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,300-80, 320, 44)];
        // 選取日期完成鈕 並給他一個 selector
        UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonSystemItemDone target:self action:@selector(bqPicker)];
        UIBarButtonItem *Flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        
        // 把按鈕加進 UIToolbar
        //154,238,225
        right.tintColor=[UIColor redColor];
        _toolbar.backgroundColor=[UIColor colorWithRed:180/255.0 green:190/255.0 blue:191/255.0 alpha:1];
        [_toolbar setItems:@[Flex,right] animated:YES];
        _pickerview = [[UIPickerView alloc] init];
        _pickerview.delegate =self;
        _pickerview.frame = CGRectMake(0, 300+44-80, 320, 180);
        
        
        [[NurseService sharedInstance]queryWard:nksdm withSuccessHandler:^(id result) {
            
            if ([result isKindOfClass:[NSArray class]])
            {
                NSMutableArray *dataArray=[NSMutableArray array];
                
                NSMutableDictionary *bqDict=[NSMutableDictionary dictionary];
                
                //                dataArray=[result objectForKey:@"科室名称"];
                for(int i = 0; i < [result count]; i++)
                {
                    
                    NSDictionary* record = [result objectAtIndex:i];
                    NSString*ksmc=[record valueForKey:@"科室名称"];
                    NSString*ksdm=[record valueForKey:@"科室代码"];
                    [dataArray addObject:ksmc];
                    
                    [bqDict setObject: ksdm forKey:ksmc];
                    
                }
                pickerData=dataArray;
                bqDictionary=bqDict;
                //NSString*dm=[ksDictionary objectForKey:@"普外科"];
                //NSString*dm1=[ksDictionary objectForKey:@"10100000"];
                [self.view addSubview:_toolbar];
                [self.view addSubview:_pickerview];
                // [self.view addSubview:lbl];
                self.lblbqxz.textColor=[UIColor colorWithRed:255/255.0 green:0/255.0 blue:0/255.0 alpha:1];
                _pickerview.backgroundColor = [UIColor whiteColor];
                
                
            }
        }andFailHandler:^(NSError *error, NSString *title) {
            [[DataEngine sharedInstance]showMessage:[error description] withTitle:title];
        }];
        
        
        self.Scrollview.scrollEnabled=false;
        
     }
    }

    
    
}

- (IBAction)Btcw:(id)sender {
    
    
    if(nbqdm==nil)
    {
        [[DataEngine sharedInstance]showMessage:@"请先选择病区!" withTitle:@"床位选择错误"];
    }
    else
    {
        [self Arrangement:@"3"];

        
    if (!_pickerview) {
        
        //[self Arrangement:@"2"];
        // 建立 UIToolbar
        _toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,360-140, 320, 44)];
        // 選取日期完成鈕 並給他一個 selector
        UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonSystemItemDone target:self action:@selector(cwPicker)];
        UIBarButtonItem *Flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        
        // 把按鈕加進 UIToolbar
        //154,238,225
        right.tintColor=[UIColor redColor];
        _toolbar.backgroundColor=[UIColor colorWithRed:180/255.0 green:190/255.0 blue:191/255.0 alpha:1];
        [_toolbar setItems:@[Flex,right] animated:YES];
        _pickerview = [[UIPickerView alloc] init];
        _pickerview.delegate =self;
        _pickerview.frame = CGRectMake(0, 360+44-140, 320, 180);
        
        
        [[NurseService sharedInstance]queryBednumber:nbqdm withSuccessHandler:^(id result) {
            
            if ([result isKindOfClass:[NSArray class]])
            {
                NSMutableArray *dataArray=[NSMutableArray array];
                
                //NSMutableDictionary *bqDict=[NSMutableDictionary dictionary];
                
                //                dataArray=[result objectForKey:@"科室名称"];
                for(int i = 0; i < [result count]; i++)
                {
                    
                    NSDictionary* record = [result objectAtIndex:i];
                    NSString*ch=[record valueForKey:@"床位号"];
                    [dataArray addObject:ch];
                    
                }
                pickerData=dataArray;
                [self.view addSubview:_toolbar];
                [self.view addSubview:_pickerview];
                // [self.view addSubview:lbl];
                self.lblcwxz.textColor=[UIColor colorWithRed:255/255.0 green:0/255.0 blue:0/255.0 alpha:1];
                _pickerview.backgroundColor = [UIColor whiteColor];
                
                
            }
        }andFailHandler:^(NSError *error, NSString *title) {
            [[DataEngine sharedInstance]showMessage:[error description] withTitle:title];
        }];
        
        
        self.Scrollview.scrollEnabled=false;
        
      }
    }

}

- (IBAction)Btsure:(id)sender {
    
    
    if(nbqdm==nil)
    {
        [[DataEngine sharedInstance]showMessage:@"请先选择病区!" withTitle:@"病人流转错误"];
    }
    else
    {
    
      NSMutableArray* array = [NSMutableArray array];
      NSDictionary* dict = [OperatorService sharedInstance].currentOperator ;
      [array addObject:zyh];
      [array addObject:bqdm];
      [array addObject:nbqdm];
      [array addObject:cwh];
      [array addObject:ncwh];
      [array addObject:ksdm];
      [array addObject:nksdm];
      [array addObject:[dict valueForKey:@"工号"]];
      NSString* postData = [array componentsJoinedByString:@";"];
    
      [[NurseService sharedInstance]postZCWithData:postData withSuccessHandler:^(id result) {
        [[DataEngine sharedInstance]showMessage:@"病人流转操作成功" withTitle:@"病人流转成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } andFailHandler:^(NSError *error, NSString *title) {
        [[DataEngine sharedInstance]showMessage:[error description] withTitle:@"病人流转失败"];
    }];
        
    }

}
@end
